/*
 * @(#)Json2ObjectInterceptor.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.interceptor.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;

import com.nhncorp.lucy.common.config.model.ApplicationInfo;
import com.nhncorp.lucy.web.interceptor.AbstractInterceptor;
import com.nhncorp.usf.core.util.StringUtil;
import com.nhncorp.usf.core.xwork.ActionInvocationHelper;
import com.opensymphony.xwork.ActionInvocation;

/**
 * {@link UsfInterceptor} to convert xml of paremter to {@link Map}.
 * {@link ApplicationInfo#getNativeEncoding(String)} must be not null. Before
 * converted data is inserted into {@link Map}, prefix part of parameterName is
 * removed. If prefix is "json_" and parameterName is "json_sample", key of
 * {@link Map} which references converted data is "sample".
 * 
 * @author Web Platform Development Team
 */
public class Json2ObjectInterceptor extends AbstractInterceptor {
    private static final long serialVersionUID = -7947392467815382484L;

    Log log = LogFactory.getLog(Json2ObjectInterceptor.class);

    /**
     * prefix of parameter-name which has xml-type value.
     */
    private String prefixOfParamter = "json_";

    /**
     * Sets the prefix of paramter.
     * 
     * @param prefixOfParamter
     *            the new prefix of paramter
     */
    public void setPrefixOfParamter(String prefixOfParamter) {
        this.prefixOfParamter = prefixOfParamter;
    }

    /**
     * replace json to converted {@link java.util.Map}.
     * 
     * @param actionInvocation
     *            the ActionInvocation
     * @return the interceptor result
     * @throws Exception
     *             for Exception
     */
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        Map<String, Object> dataMap = ActionInvocationHelper
                .getDataMap(actionInvocation);
        Map<String, Object> jsonMap = new HashMap<String, Object>();

        for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
            String parameterName = entry.getKey();

            if (!parameterName.startsWith(prefixOfParamter)) {
                continue;
            }

            Object jsonObj = entry.getValue();

            try {
                ObjectMapper objectMapper = new ObjectMapper();
                Object jsonObject = objectMapper.readValue((String) jsonObj,
                        Object.class);
                jsonMap.put(StringUtil.substringAfter(parameterName,
                        prefixOfParamter), jsonObject);

                if (log.isDebugEnabled()) {
                    log.debug("JSON(" + jsonObject.toString()
                            + ") is convertd to Object(" + jsonObject + ").");
                }
            } catch (Exception except) {
                log.error("JSON(" + jsonObj + ") is not convertd to Object.",
                        except);
            }
        }

        dataMap.putAll(jsonMap);

        return actionInvocation.invoke();
    }
}
